<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="工作流名称" prop="flowName">
        <el-input v-model="queryParams.flowName" placeholder="请输入工作流名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工作流类型" prop="flowType">
        <el-select v-model="queryParams.flowType" placeholder="请选择工作流类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="工作流返回类型" prop="flowRetType">
        <el-select v-model="queryParams.flowRetType" placeholder="请选择工作流返回类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="返回值是否为数组" prop="flowRetIsArray">
        <el-input v-model="queryParams.flowRetIsArray" placeholder="请输入返回值是否为数组" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"> 搜索 </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"> 重置 </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:workflowMain:add']" type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"> 新增 </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:workflowMain:edit']" type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"> 修改 </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:workflowMain:remove']" type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"> 删除 </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:workflowMain:export']" type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"> 导出 </el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="workflowMainList" :default-sort="{prop: 'flowId', order: 'descending'}" @selection-change="handleSelectionChange" @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工作流编号" align="center" prop="flowId" sortable="custom" />
      <el-table-column label="工作流名称" align="center" prop="flowName" sortable="custom" />
      <el-table-column label="工作流类型" align="center" prop="flowType" sortable="custom" />
      <el-table-column label="工作流返回类型" align="center" prop="flowRetType" sortable="custom" />
      <el-table-column label="返回值是否为数组" align="center" prop="flowRetIsArray" sortable="custom" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['system:workflowMain:edit']" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"> 修改 </el-button>
          <el-button v-hasPermi="['system:workflowMain:remove']" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改工作流主体对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工作流名称" prop="flowName">
          <el-input v-model="form.flowName" placeholder="请输入工作流名称" />
        </el-form-item>
        <el-form-item label="工作流类型" prop="flowType">
          <el-select v-model="form.flowType" placeholder="请选择工作流类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="工作流返回类型" prop="flowRetType">
          <el-select v-model="form.flowRetType" placeholder="请选择工作流返回类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="返回值是否为数组" prop="flowRetIsArray">
          <el-input v-model="form.flowRetIsArray" placeholder="请输入返回值是否为数组" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm"> 确 定 </el-button>
        <el-button @click="cancel"> 取 消 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listWorkflowMain, getWorkflowMain, delWorkflowMain, addWorkflowMain, updateWorkflowMain, exportWorkflowMain} from '@/api/system/workflowMain'

export default {
  name: 'WorkflowMain',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作流主体表格数据
      workflowMainList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flowName: null,
        flowType: null,
        flowRetType: null,
        flowRetIsArray: null,
        orderByColumn: 'flowId',
        isAsc: 'desc'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        flowName: [{required: true, message: '工作流名称不能为空', trigger: 'blur'}],
        flowType: [{required: true, message: '工作流类型不能为空', trigger: 'change'}],
        flowRetType: [
          {
            required: true,
            message: '工作流返回类型不能为空',
            trigger: 'change'
          }
        ],
        flowRetIsArray: [
          {
            required: true,
            message: '返回值是否为数组不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询工作流主体列表 */
    getList() {
      this.loading = true
      listWorkflowMain(this.queryParams).then(response => {
        this.workflowMainList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        flowId: null,
        flowName: null,
        flowType: null,
        flowRetType: null,
        flowRetIsArray: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 更改排序方式 */
    handleSortChange({prop, order}) {
      this.queryParams.orderByColumn = prop
      this.queryParams.isAsc = order === 'descending' ? 'desc' : 'asc'
      this.handleQuery()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.flowId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加工作流主体'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const flowId = row.flowId || this.ids
      getWorkflowMain(flowId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改工作流主体'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          ;(() => {
            this.loading = true
            if (this.form.flowId) {
              return updateWorkflowMain(this.form)
            }
            return addWorkflowMain(this.form)
          })()
            .then(response => {
              if (response.code === 200) {
                this.msgSuccess((this.form.flowId ? '修改' : '新增') + '成功')
                this.open = false
                this.getList()
              }
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const flowIds = row.flowId || this.ids
      this.$confirm('是否确认删除工作流主体编号为"' + flowIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function () {
          return delWorkflowMain(flowIds)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有工作流主体数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function () {
          return exportWorkflowMain(queryParams)
        })
        .then(response => {
          this.download(response.msg)
        })
    }
  }
}
</script>
